package com.ebao.action.base;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;

/*
 * 提供了2种获取request、session的方法
 */
public class BaseAction extends ActionSupport implements RequestAware, SessionAware {

	private static final long serialVersionUID = 3637135435910790708L;
	
	protected transient final Logger log = LoggerFactory.getLogger(getClass());

	/**
	 * Constant for custom error messages stored in session.
	 */
	protected static final String ERRORS_IN_SESSION = "errors";
	
	/**
	 * Constant for custom info messages stored in session.
	 */
	protected static final String INFORMATION_IN_SESSION = "information";
	/**
	 * Constant for custom success messages stored in session.
	 */
	protected static final String MESSAGES_IN_SESSION = "messages";
	/**
	 * Constant for custom warning messages stored in session.
	 */
	protected static final String WARNING_IN_SESSION = "warning";

	protected Map<String, Object> request;
	protected Map<String, Object> session;
	
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}
	
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	public void addActionError(String errorMsg) {
		// super.addActionError(errorMsg);
		@SuppressWarnings("unchecked")
		List<String> messages = (List<String>) getSession().getAttribute(ERRORS_IN_SESSION);
		if (messages == null) {
			messages = new ArrayList<String>();
		}
		messages.add(errorMsg);
		getSession().setAttribute(ERRORS_IN_SESSION, messages);
	}
	
	public void addActionInformation(String info) {
		// super.addActionMessage(msg);
		@SuppressWarnings("unchecked")
		List<String> messages = (List<String>) getSession().getAttribute(INFORMATION_IN_SESSION);
		if (messages == null) {
			messages = new ArrayList<String>();
		}
		messages.add(info);
		getSession().setAttribute(INFORMATION_IN_SESSION, messages);
	}
	
	public void addActionMessage(String msg) {
		// super.addActionMessage(msg);
		@SuppressWarnings("unchecked")
		List<String> messages = (List<String>) getSession().getAttribute(MESSAGES_IN_SESSION);
		if (messages == null) {
			messages = new ArrayList<String>();
		}
		messages.add(msg);
		getSession().setAttribute(MESSAGES_IN_SESSION, messages);
	}
	
	public void addActionWarning(String warning) {
		@SuppressWarnings("unchecked")
		List<String> messages = (List<String>) getSession().getAttribute(WARNING_IN_SESSION);
		if (messages == null) {
			messages = new ArrayList<String>();
		}
		messages.add(warning);
		getSession().setAttribute(WARNING_IN_SESSION, messages);
	}
	
	public void clearActionMessage() {
		@SuppressWarnings("unchecked")
		List<String> messages = (List<String>) getSession().getAttribute(MESSAGES_IN_SESSION);
		if (messages != null) {
			messages.clear();
		}
	}
	
	/**
	 * 获取request
	 * 
	 * @return
	 */
	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * 获取session
	 * 
	 * @return
	 */
	protected HttpSession getSession() {
		return ServletActionContext.getRequest().getSession();
	}

	/**
	 * 获取response
	 * 
	 * @return
	 */
	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	
	/**
	 * 向客户端回写内容
	 * 
	 * @param msg
	 */
	protected void writeResponseMsg(String msg) {
		try {
			getResponse().setContentType("text/plain");
			getResponse().getWriter().print(msg);
			getResponse().flushBuffer();
		} catch (IOException e) {
			log.error(e.getMessage());
		}
	}

	protected void writeResponseMsgAsHtml(String msg) {
		try {
			getResponse().setContentType("text/html");
			getResponse().getWriter().print(msg);
			getResponse().flushBuffer();
		} catch (IOException e) {
			log.error(e.getMessage());
		}
	}
	
}
